<template>
  <div class="Bread">
  <Bread></Bread>
  </div>
  <div>
    <Pmsproduct @SearchEmit="formInfo"> </Pmsproduct>
    <div class="adddata">
      <div class="title">
        <el-icon><Tickets /></el-icon>
        <h3>数据列表</h3>
      </div>

      <div>
        <el-button type="primary" @click="addProductCateFn">添加</el-button>
      </div>
    </div>
    <div class="tables">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        @select="onSelect"
        @select-all="onSelectAll"
      >
        <el-table-column width="60" align="center" type="selection"> </el-table-column>
        <el-table-column prop="id" label="编号" width="90" align="center" />
        <el-table-column label="商品图片" align="center" width="140">
          <template #default="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.pic"
              fit="fill"
            />
          </template>
        </el-table-column>
        <el-table-column prop="name" label="商品名称" align="center" width="100">
          <template #default="scope">
            <span>{{ scope.row.name }}</span>
            <p>品牌:{{ scope.row.brandName }}</p>
          </template>
        </el-table-column>

        <el-table-column prop="firstLetter" label="价格/货号" width="100" align="center">
          <template #default="scope">
            <span>价格:￥{{ scope.row.price }}</span>
            <p>货号:{{ scope.row.productSn }}</p>
          </template>
        </el-table-column>
        <el-table-column label="标签" width="120" align="center">
          <template #default="scope">
            <div>
              <el-switch
                :value="scope.row.publishStatus === 1 ? true : false"
                @click="publishStatusFn(scope.row.id, scope.row.publishStatus)"
                inactive-text="上架:"
              />
              <el-switch
                :value="scope.row.newStatus === 1 ? true : false"
                @click="newStatusFn(scope.row.id, scope.row.newStatus)"
                inactive-text="新品:"
              />
              <el-switch
                :value="scope.row.recommandStatus === 1 ? true : false"
                @click="recommandStatusFn(scope.row.id, scope.row.recommandStatus)"
                inactive-text="推荐:"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" width="100" align="center" />
        <el-table-column label="SKU库存" width="100" align="center">
          <template #default="scope">
            <div class="icon_pen" @click="changevisiableFn">
              <el-icon><EditPen /></el-icon>
            </div>
          </template>
        </el-table-column>

        <el-table-column
          prop="sale"
          label="销量"
          width="98"
          align="center"
        ></el-table-column>
        <el-table-column label="审核状态" width="120" align="center">
          <template #default="scope">
            <div>
              <p>{{ scope.row.verifyStatus == 1 ? "审核通过" : "未审核" }}</p>
              <p class="text">审核详情</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template #default="scope">
            <div>
              <el-button @click="">编辑</el-button>
              <el-button type="danger" @click="deltProductFn(scope.row.id)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="showgoods">
        <el-select v-model="showvalue" class="m-2" placeholder="批量操作">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button type="primary" @click="SelectAllFn"> 确定 </el-button>
      </div>
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 15, 20]"
          :small="small"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <Editproducth :visiable="visiable"  @getvisiable="visiableFn" @confirm="onConfirmDialog"></Editproducth>
  </div>
</template>

<script setup lang="ts">
import Pmsproduct from "@/components/_l_Pms/Pmsproduct.vue";
import { useRouter } from "vue-router";
import { ref } from "vue";
import type { Ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import * as api from "@/request/api";
import Editproducth from "./Editproduct.vue"
import Bread from "@/components/Bread.vue";
//列表数据
let tableData = ref([]) as Ref<any>;
let router = useRouter();
//添加路由跳转
let addProductCateFn = () => {
  router.push({
    path: "/pms/addProduct",
  });
};
//分页数据
const currentPage = ref(1);
const pageSize = ref(5);
const total = ref(0);
const small = ref(false);
const background = ref(false);
const disabled = ref(false);
//进入页面发送请求
let getGoodsBrand = async () => {
  let data = await api.goods.ProductList({
    pageNum: currentPage.value,
    pageSize: pageSize.value,
  });
  // console.log(data);
  currentPage.value = data.pageNum;
  pageSize.value = data.pageSize;
  total.value = data.total;
  tableData.value = data.list;
};
getGoodsBrand();
//分页事件
const handleSizeChange = (val: number) => {
  getGoodsBrand();
};
const handleCurrentChange = (val: number) => {
  getGoodsBrand();
};
//批量操作数据
const options = [
  {
    value: 0,
    label: "商品上架",
  },
  {
    value: 1,
    label: "商品下架",
  },
  {
    value: 2,
    label: "设为推荐",
  },
  {
    value: 3,
    label: "取消推荐",
  },
  {
    value: 4,
    label: "设为新品",
  },
  {
    value: 5,
    label: "取消新品",
  },
  {
    value: 6,
    label: "转移到分类",
  },
  {
    value: 7,
    label: "移入回收站",
  },
];
//显示隐藏商品分类的数据
const showvalue = ref(null);
//单选全选显示隐藏
let ids = ref([]);
let onSelect = (e: any) => {
  // console.log(e);
  let newData = e.map((el) => {
    return el.id;
  });
  // console.log(newData);
  ids.value = newData;
  // console.log(ids.value);
};
let onSelectAll = (e: any) => {
  // console.log("onSelectAll", e);
  let newData = e.map((el) => {
    return el.id;
  });
  // console.log(newData);
  ids.value = newData;
  // console.log(ids.value);
};
// console.log("showvalue", showvalue);

//发送查询请求
let formInfo = async (e) => {
  let data = await api.goods.ProductList(e);
  // console.log(data);
  currentPage.value = data.pageNum;
  pageSize.value = data.pageSize;
  total.value = data.total;
  tableData.value = data.list;
};
//标签功能
let updateStatus = async (val: number | number[], val2: number, api: any) => {
  if (val2 == 1) {
    val2 = 0;
  } else if (val2 == 0) {
    val2 = 1;
  }
  let newData = await api({
    ids: val,
    showStatus: val2,
  });
  if (newData) {
    ElMessage({
      message: "操作成功",
      grouping: true,
      type: "success",
    });
    getGoodsBrand();
  } else {
    ElMessage({
      grouping: true,
      message: "操作失败",
      type: "error",
    });
  }
};
//上加状态
let publishStatusFn = (val: number | number[], val2: number) => {
  updateStatus(val, val2, api.goods.UpdatePublishStatus);
};
//新品状态
let newStatusFn = (val: number | number[], val2: number) => {
  updateStatus(val, val2, api.goods.UpdatenewStatus);
};
//推荐状态
let recommandStatusFn = (val: number | number[], val2: number) => {
  updateStatus(val, val2, api.goods.UpdaterecommendStatus);
};
//删除逻辑
let deltProductFn = (val: number) => {
  // console.log(row);  // row就是表单数据
  // 二次 确认
  ElMessageBox.confirm("是否删除该属性", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      console.log(val);

      await api.goods.DeleteProduct({
        ids: val,
      });
      getGoodsBrand();
      ElMessage({
        type: "success",
        message: "操作成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消提交",
      });
    });
};
//批量操作功能
let SelectAllFn = async () => {
  if (showvalue.value == null) {
    ElMessage({
      message: "请选取批量操作的类型",
      grouping: true,
      type: "warning",
    });
  } else if (ids.value.length == 0) {
    ElMessage({
      message: "请至少选择一条数据",
      grouping: true,
      type: "warning",
    });
  } else if (showvalue.value == 0) {
    publishStatusFn(ids.value, 0);
  } else if (showvalue.value == 1) {
    publishStatusFn(ids.value, 1);
  } else if (showvalue.value == 2) {
    recommandStatusFn(ids.value,0)
  } else if (showvalue.value == 3) {
    recommandStatusFn(ids.value,1)
  } else if (showvalue.value == 4) {
    newStatusFn(ids.value,0)
  } else if (showvalue.value == 5) {
    newStatusFn(ids.value,1)
  } else if (showvalue.value == 6) {
    ElMessage({
      message: "功能未开放",
      grouping: true,
      type: "warning",
    });
    return
  } else if (showvalue.value == 7) {
    ElMessage({
      message: "功能未开放",
      grouping: true,
      type: "warning",
    });
    return
  }
};
//suk页面
let visiable = ref(false);
let changevisiableFn = () =>{
  visiable.value = true;
}
const visiableFn = (e: boolean) => {
  console.log("e", e);
  visiable.value = e;
};
let onConfirmDialog = () => {
    // console.log(row);  // row就是表单数据
    // 二次 确认 
    ElMessageBox.confirm(
        '是否确认提交!', "提示",
        {
            confirmButtonText: 'OK',
            cancelButtonText: 'Cancel',
            type: 'warning',
        }
    )
        .then(async () => {
            // // 发请求
            // if(add){
            //   // 去判断 是 编辑 还是添加
            //   await api.admin.adminRegister(row)//新增
            // }else{
            //   await api.admin.adminUpdate(row) // 编辑
            // }
            
            ElMessage({
                type: 'success',
                message: '操作成功',
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消提交',
            })
        })
}
</script>

<style lang="scss" scoped>
.adddata {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  margin-bottom: 20px;
  border: 1px solid #ebeef5;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    h3 {
      color: #303133;
      font-size: 14px;
      margin-left: 10px;
    }
  }
}
.pagination {
  margin: 20px 0;
  padding-right: 20px;
  float: right;
}
.showgoods {
  margin: 20px 0;
  padding-right: 20px;
  float: left;
  .el-button {
    margin-left: 15px;
  }
}
.icon_pen{
  width: 50px;
  height: 50px;
  background-color: #409eff;
  border-radius: 50%;
  text-align: center;
  line-height: 50px;
  font-size: 14px;
  color: #ebeef5;
  margin: auto;
  &:hover{
    background-color: rgba(64, 158, 255,0.7);
  }
}
.text{
  font-size: 14px;
  color: #409eff;
  cursor: pointer;
  &:hover{
    color: rgba(64, 158, 255,0.6);
  }
}
.Bread {
  position: sticky;
  top: -100px;
  margin: 0;
  width: 100%;
  border-bottom: 1px solid #eee;
  // box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
  padding-left: 20px;
  box-sizing: border-box;
}
</style>
